package cn.iocoder.yudao.module.system.controller.admin.problem;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.problem.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.problem.ProblemDO;
import cn.iocoder.yudao.module.system.service.problem.ProblemService;

@Tag(name = "管理后台 - 问题列")
@RestController
@RequestMapping("/system/problem")
@Validated
public class ProblemController {

    @Resource
    private ProblemService problemService;

    @PostMapping("/create")
    @Operation(summary = "创建问题列")
    @PreAuthorize("@ss.hasPermission('system:problem:create')")
    public CommonResult<Long> createProblem(@Valid @RequestBody ProblemSaveReqVO createReqVO) {
        return success(problemService.createProblem(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新问题列")
    @PreAuthorize("@ss.hasPermission('system:problem:update')")
    public CommonResult<Boolean> updateProblem(@Valid @RequestBody ProblemSaveReqVO updateReqVO) {
        problemService.updateProblem(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除问题列")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:problem:delete')")
    public CommonResult<Boolean> deleteProblem(@RequestParam("id") Long id) {
        problemService.deleteProblem(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得问题列")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:problem:query')")
    public CommonResult<ProblemRespVO> getProblem(@RequestParam("id") Long id) {
        ProblemDO problem = problemService.getProblem(id);
        return success(BeanUtils.toBean(problem, ProblemRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得问题列分页")
    @PreAuthorize("@ss.hasPermission('system:problem:query')")
    public CommonResult<PageResult<ProblemRespVO>> getProblemPage(@Valid ProblemPageReqVO pageReqVO) {
        PageResult<ProblemDO> pageResult = problemService.getProblemPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProblemRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出问题列 Excel")
    @PreAuthorize("@ss.hasPermission('system:problem:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProblemExcel(@Valid ProblemPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProblemDO> list = problemService.getProblemPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "问题列.xls", "数据", ProblemRespVO.class,
                        BeanUtils.toBean(list, ProblemRespVO.class));
    }

}